package com.srwl.mytx.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

public class NetworkUtil {

    public static boolean isNetworkAvailable(Context context) {
        NetworkInfo networkInfo = getNetworkInfo(context);
        return networkInfo == null ? false : networkInfo.isConnected();
    }

    public static boolean isWifi(Context context) {
        NetworkInfo networkInfo = getNetworkInfo(context);
        if (networkInfo != null && networkInfo.isAvailable()) {
            return networkInfo.getType() == 1 && networkInfo.getState() == NetworkInfo.State.CONNECTED;
        } else {
            return false;
        }
    }

    public static NetworkInfo getNetworkInfo(Context context) {
        try {
            ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo == null && Build.VERSION.SDK_INT >= 23) {
                Network network = connManager.getActiveNetwork();
                networkInfo = connManager.getNetworkInfo(network);
            }

            return networkInfo;
        } catch (Exception var4) {
            return null;
        }
    }

    public static WifiInfo getWifiInfo(Context context) {
        WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info;
    }
}
